﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class CCDiscrepanceRule : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkQtyGtrZero;
        protected CheckBox chkSysCreate;
        protected DropDownList ddlNegAdjExpiryDate;
        protected DropDownList ddlNegAdjLot;
        protected DropDownList ddlNegAdjProduceDate;
        protected DropDownList ddlNegAdjReceiptDate;
        protected DropDownList ddlPosAdjExpiryDate;
        protected DropDownList ddlPosAdjLot;
        protected DropDownList ddlPosAdjProduceDate;
        protected DropDownList ddlPosAdjReceiptDate;
        protected HtmlForm F2215;
        protected HtmlInputHidden hdnFrom;
        protected Label lbCcAdjRuleID;
        protected Label lbl1;
        protected Label lbl2;
        protected Label lblDescr;
        protected Label lblMsg;
        protected Label lblNegAdjExpiryDate;
        protected Label lblNegAdjLot;
        protected Label lblNegAdjProduceDate;
        protected Label lblNegAdjReceiptDate;
        protected Label lblOwner;
        protected Label lblPosAdjExpiryDate;
        protected Label lblPosAdjLot;
        protected Label lblPosAdjProduceDate;
        protected Label lblPosAdjReceiptDate;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCcAdjRuleID;
        protected TextBox txtDescr;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtCcAdjRuleID.Text = "";
            this.txtCcAdjRuleID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtCcAdjRuleID.Text.Trim()))
            {
                try
                {
                    List<string> strCcAdjRuleIDs = new List<string> {
                        this.txtCcAdjRuleID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(strCcAdjRuleIDs, "CYCLE_COUNT_RULE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("CCDiscrepanceRuleQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("CCDiscrepanceRuleList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtCcAdjRuleID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2455_001");
            }
            else if (string.IsNullOrEmpty(this.txtDescr.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2455_002");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateCycleCountRuleInfo();
            }
            else
            {
                this.InsertCycleCountRuleInfo();
            }
        }

        private CycleCountRuleInfo GetCycleCountRuleInfo(CycleCountRuleInfo cycleCountRuleInfo)
        {
            CycleCountRuleInfo cycleCountRule = cycleCountRuleInfo;
            cycleCountRule.Descr = this.txtDescr.Text.Trim();
            cycleCountRule.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            cycleCountRule.SystemCreated = this.chkSysCreate.Checked ? "Y" : "N";
            cycleCountRule.QtyGtrZero = this.chkQtyGtrZero.Checked ? "Y" : "N";
            cycleCountRule.NegAdjLot = this.ddlNegAdjLot.SelectedValue;
            cycleCountRule.NegAdjProduceDate = this.ddlNegAdjProduceDate.SelectedValue;
            cycleCountRule.NegAdjExpiryDate = this.ddlNegAdjExpiryDate.SelectedValue;
            cycleCountRule.NegAdjReceiptDate = this.ddlNegAdjReceiptDate.SelectedValue;
            cycleCountRule.PosAdjLot = this.ddlPosAdjLot.SelectedValue;
            cycleCountRule.PosAdjProduceDate = this.ddlPosAdjProduceDate.SelectedValue;
            cycleCountRule.PosAdjExpiryDate = this.ddlPosAdjExpiryDate.SelectedValue;
            cycleCountRule.PosAdjReceiptDate = this.ddlPosAdjReceiptDate.SelectedValue;
            return cycleCountRule;
        }

        private void InsertCycleCountRuleInfo()
        {
            try
            {
                CycleCountRuleInfo cycleCountRule = new CycleCountRuleInfo();
                cycleCountRule = this.GetCycleCountRuleInfo(cycleCountRule);
                cycleCountRule.CcAdjRuleID = this.txtCcAdjRuleID.Text.Trim();
                cycleCountRule.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(cycleCountRule, "CYCLE_COUNT_RULE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = cycleCountRule;
                this.txtCcAdjRuleID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            List<DropDownList> ddls = new List<DropDownList> {
                this.ddlNegAdjLot,
                this.ddlNegAdjProduceDate,
                this.ddlNegAdjReceiptDate,
                this.ddlNegAdjExpiryDate,
                this.ddlPosAdjLot,
                this.ddlPosAdjProduceDate,
                this.ddlPosAdjReceiptDate,
                this.ddlPosAdjExpiryDate
            };
            base.BindData2DDLBySysCode(ddls, "CCLotAdjRule");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetCycleCountRuleInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetCycleCountRuleInfo2Form()
        {
            try
            {
                string strCcAdjRuleID = base.Request.QueryString["ID"].ToString();
                CycleCountRuleInfo cycleCountRule = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(strCcAdjRuleID, "CYCLE_COUNT_RULE") as CycleCountRuleInfo;
                if (cycleCountRule != null)
                {
                    base.DataEntityViewState = cycleCountRule;
                    this.txtCcAdjRuleID.Text = cycleCountRule.CcAdjRuleID;
                    this.txtCcAdjRuleID.Enabled = false;
                    this.txtDescr.Text = cycleCountRule.Descr;
                    this.chkIsActive.Checked = cycleCountRule.IsActive == "Y";
                    this.chkSysCreate.Checked = cycleCountRule.SystemCreated == "Y";
                    this.chkQtyGtrZero.Checked = cycleCountRule.QtyGtrZero == "Y";
                    this.ddlNegAdjLot.SelectedValue = cycleCountRule.NegAdjLot;
                    this.ddlNegAdjProduceDate.SelectedValue = cycleCountRule.NegAdjProduceDate;
                    this.ddlNegAdjExpiryDate.SelectedValue = cycleCountRule.NegAdjExpiryDate;
                    this.ddlNegAdjReceiptDate.SelectedValue = cycleCountRule.NegAdjReceiptDate;
                    this.ddlPosAdjLot.SelectedValue = cycleCountRule.PosAdjLot;
                    this.ddlPosAdjProduceDate.SelectedValue = cycleCountRule.PosAdjProduceDate;
                    this.ddlPosAdjExpiryDate.SelectedValue = cycleCountRule.PosAdjExpiryDate;
                    this.ddlPosAdjReceiptDate.SelectedValue = cycleCountRule.PosAdjReceiptDate;
                    if (this.chkSysCreate.Checked)
                    {
                        this.chkIsActive.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateCycleCountRuleInfo()
        {
            try
            {
                CycleCountRuleInfo cycleCountRule = base.DataEntityViewState as CycleCountRuleInfo;
                cycleCountRule = this.GetCycleCountRuleInfo(cycleCountRule);
                cycleCountRule.UpdatedBy = cycleCountRule.CreatedBy;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(cycleCountRule, "CYCLE_COUNT_RULE") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

